package com.aaa.marketresearch.webclient.gwt.admin.design;

import com.aaa.marketresearch.webclient.gwt.admin.widget.BillTable;
import com.aaa.marketresearch.webclient.gwt.admin.widget.FamilyTable;
import com.aaa.marketresearch.webclient.gwt.admin.widget.GoodsTable;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Nail
 */
public class AdminPage extends Composite {

    interface AdminPageUiBinder extends UiBinder<Widget, AdminPage> { }
    private static AdminPageUiBinder uiBinder = GWT.create(AdminPageUiBinder.class);

    private static AdminPage instance = null;

    public static AdminPage getInstance() {
        if (instance == null) {
            instance = new AdminPage();
        }
        return instance;
    }

    @UiField
    protected FocusPanel navigationPanelFamilyTab;

    @UiField
    protected FocusPanel navigationPanelGoodsTab;

    @UiField
    protected FocusPanel navigationPanelBillTab;

    @UiField
    protected FlowPanel contentPanel;

    private List<FocusPanel> navigationPanelTabList = new ArrayList<>();

    protected FocusPanel lastNavigationPanelActiveTab;

    private AdminPage() {
        // initialize
        initWidget(uiBinder.createAndBindUi(this));
        // navigation panel
        {
            navigationPanelTabList.add(navigationPanelFamilyTab);
            navigationPanelTabList.add(navigationPanelGoodsTab);
            navigationPanelTabList.add(navigationPanelBillTab);

            ClickHandler tabClick = new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    FocusPanel clickTab = (FocusPanel) event.getSource();
                    AdminPage.this.setNavigationPanelActiveTab(clickTab);
                }
            };

            for (FocusPanel tab : navigationPanelTabList) {
                tab.addClickHandler(tabClick);
            }

            this.setNavigationPanelActiveTab(navigationPanelTabList.get(0));
        }

    }

    private void setNavigationPanelActiveTab(FocusPanel activeTab) {
        if (this.lastNavigationPanelActiveTab != activeTab) {
            if (this.lastNavigationPanelActiveTab != null) {
                this.lastNavigationPanelActiveTab.removeStyleName("navigation-panel-tab-active");
            }
            for (FocusPanel tab : navigationPanelTabList) {
                if (activeTab == tab) {
                    activeTab.addStyleName("navigation-panel-tab-active");
                }
            }

            this.contentPanel.clear();
            if (activeTab == this.navigationPanelFamilyTab) {
                this.contentPanel.add(FamilyTable.getInstance());
                FamilyTable.getInstance().loadData();
            } else if (activeTab == navigationPanelGoodsTab) {
                this.contentPanel.add(GoodsTable.getInstance());
                GoodsTable.getInstance().loadData();
            } else if (activeTab == navigationPanelBillTab) {
                this.contentPanel.add(BillTable.getInstance());
                BillTable.getInstance().loadData();
            }

            this.lastNavigationPanelActiveTab = activeTab;
        }
    }
}
